<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button type="primary" @click="add">+ 新增角色</el-button>
          <!-- 
         el-table  
             data:绑定数据（数组数据类型）
             el-table-column label="标题" prop:对应字段名 width
                自定义表格
                  template v-slot="scope"
                    scope:{
                      $index:索引
                      row:当前行数据
                    }
           -->
          <el-table :data="list">
            <el-table-column label="序号">
              <template v-slot="{ $index }">
                <div>
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name"> </el-table-column>
            <el-table-column label="描述" prop="description"> </el-table-column>
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button type="text" @click="setPower(row)"
                    >分配权限</el-button
                  >
                  <el-button type="text" @click="edit(row)">修改</el-button>
                  <el-button type="text" @click="del(row)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination">
            <!-- 
              @size-change:页容量改变时执行
              @current-change:页码改变时执行
              current-page：默认页码
              page-sizes:页容量选项
              page-size:默认页容量
              layout：功能列表
              total：总数据

             -->
            <el-pagination
              @size-change="sizeChange"
              @current-change="currChange"
              :current-page="page.page"
              :page-sizes="[1, 10, 100, 200, 300, 400]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
            >
            </el-pagination>
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" lazy>
          <!-- 动态组件
          用法：
            component :is="'info'"
          
           -->
          <component :is="'Info'"></component>
          <!-- <Info></Info> -->
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- <DialogItem :isShow="show" @input="show = $event"></DialogItem> -->
    <!-- <DialogItem v-model="show"></DialogItem> -->
    <!-- <DialogItem :isShow="show" @update:isShow="show = $event"></DialogItem> -->
    <DialogItem
      ref="dialogItem"
      :isShow.sync="show"
      :list="list"
      @currChange="currChange"
      @getData="getData"
    ></DialogItem>
    <!-- 分配权限 -->
    <DialogPower :isShow.sync="showPower" ref="dialogPower"></DialogPower>
  </div>
</template>
<script>
import { sysRole, sysRoleDel } from '@/api/setting.js'
import DialogItem from './components/dialogItem.vue'
// import Info from './components/info.vue'
import DialogPower from './components/dialogPower.vue'
export default {
  components: {
    DialogItem,
    DialogPower,
    Info: () => import('./components/info.vue') // 异步组件
  },
  data () {
    return {
      show: false,
      showPower: false,
      page: {
        page: 1,
        pagesize: 10,
        total: 100
      },
      list: []
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await sysRole(this.page)
      this.page.total = res.data.total
      this.list = res.data.rows
      console.log('角色列表数据', res)
    },
    // 页码改变
    currChange (page) {
      this.page.page = page
      this.getData()
    },
    // 页容量改变
    sizeChange (size) {
      this.page.pagesize = size
      this.page.page = 1
      this.getData()
    },
    // 新增
    add () {
      this.show = true
      this.$refs.dialogItem.mode = 'add'
    },
    // 删除
    del (row) {
      this.$confirm('请确定删除该角色吗？', '提示')
        .then(async () => {
          await sysRoleDel(row.id)
          this.$message.success('删除成功')
          this.currChange(1)
          // this.getData()
        })
        .catch(() => {})
    },
    edit (row) {
      /*
编辑：
1：打开弹框
2：数据回显
3：加入一个参数用于区分新增 与编辑
4：表单验证处理
5：调用编辑接口
6：调用完成关闭弹窗，刷新数据，提示
*/

      this.show = true
      this.$refs.dialogItem.form = JSON.parse(JSON.stringify(row))
      this.$refs.dialogItem.mode = 'edit'
    },
    // 分配权限
    setPower (row) {
      this.showPower = true
      // 调用角色详情
      this.$refs.dialogPower.getRoleInfo(row.id)
    }
  }
}
</script>
<style lang="scss" scoped>
.setting {
  padding: 15px;
  .pagination {
    text-align: right;
    padding-top: 15px;
  }
}
</style>
